﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MockPrjTeam2.Biz;
using MockPrjTeam2.Entity;

namespace MockPrjTeam2.Pages.Forms
{
    public partial class PopupListContacts1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                ShowContact();
        }

        protected void btSearch_Click(object sender, EventArgs e)
        {
            var listContact = new List<Contact>();
            var find = new ContactBiz();

            listContact = find.getAllContactByKey(txtFirstName.Text.ToString(),txtSurName.Text.ToString(),true);

            listContact = find.getAllContactByKey(txtFirstName.Text.ToString(), txtSurName.Text.ToString(),true);

            if (listContact != null)
            {
                GVSearchContact.DataSource = listContact;
                DataBind();
            }
        }
        private void ShowContact()
        {
            var listContact = new List<Contact>();
            var find = new ContactBiz();

            listContact = find.getAllContactByKey(string.Empty, string.Empty,true);

            listContact = find.getAllContactByKey(string.Empty, string.Empty, true);

            if (listContact != null)
            {
                GVSearchContact.DataSource = listContact;
                DataBind();
            }

        }
        protected void btSelect_Click(object sender, EventArgs e)
        {
            string selectedValue = Request.Form["MyRadioButton"];
            var contact = new Contact();
            var getbyId = new ContactBiz();
            contact = getbyId.getById(Convert.ToInt32(selectedValue));
            txtFirstName.Text = Convert.ToString(contact.FirstName);
            txtSurName.Text = Convert.ToString(contact.SurName);
            hdnContactId.Value = Convert.ToString(contact.ContactId);
            Session["contact1"] = txtFirstName.Text + ' ' + txtSurName.Text;
            StringBuilder script = new StringBuilder();
            script.Append("<script type='text/javascript'>");

            script.AppendFormat("window.opener.document.getElementById('txtContact1').value  = '{0}';",
                txtFirstName.Text + ' ' + txtSurName.Text);
            script.AppendFormat("window.opener.document.getElementById('hdnContactId1').value  = '{0}';",
                hdnContactId.Value);
            script.Append("this.close();");
            script.Append("</script>");
            ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "key01", script.ToString(), false);
        }
    }
}